package de.baumann.browser.activitys.node

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.Menu
import android.view.MenuItem
import android.view.View
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.activitys.AbstractShareActivity
import de.baumann.browser.api.net.vo.Line
import de.baumann.browser.api.net.vo.Pie
import de.baumann.browser.iview.INodeDetailView
import de.baumann.browser.present.NodeDetailPresenter
import kotlinx.android.synthetic.main.activity_node_detail.*
import kotlinx.android.synthetic.main.include_tool_bar.*


/**
 *@author:
 *@date:2019/9/27 14:23
 *@discription: 节点详情
 */
@Deprecated("5.0.0版本之后活动结束，不再适用")
class NodeDetailActivity : AbstractShareActivity<INodeDetailView, NodeDetailPresenter>(), INodeDetailView {

    companion object {
        fun startNodeDetailActivity(context: Context, nodeId: String) {
            context.startActivity(Intent(context, NodeDetailActivity::class.java).apply {
                putExtra("nodeId", nodeId)
            })
        }
    }

    override fun getTitleText(): String {
        return "节点详情"
    }

    override fun getLayout(): Int {
        return R.layout.activity_node_detail
    }

    override fun initView() {
        initToolbar(commToolBar,commTitle)
        btnInviteIntoNode.setOnClickListener {
            displaySharePanel()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_members, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        NodeMemberActivity.startNodeMemberActivity(mContext, getNodeId())
        return super.onOptionsItemSelected(item)
    }

    override fun initData() {
        presenter.nodeDetail(getNodeId())
    }

    override fun createPresenter(): NodeDetailPresenter {
        return NodeDetailPresenter()
    }

    override fun createView(): INodeDetailView {
        return this
    }

    fun getNodeId(): String {
        return intent.getStringExtra("nodeId")
    }


    override fun setHead(url: String) {
        OdinGlideModule.loadWithCropAndCircle(mContext, url, ivNDHead, R.drawable.icon_head)
    }

    override fun setNodeMembers(members: String) {
        tvNDTP.text = "总人数$members"
    }

    override fun setNodeOwner(name: String) {
        tvNDON.text = name
    }

    override fun setOwnerId(ownerId: String) {
    }

    override fun setCreateTime(time: String) {
        tvNDCT.text = "创建时间:$time"
    }

    override fun setTotalHash(hash: String) {
        tvNDTH.text = "总算力$hash"
    }

    override fun setPieChart(pies: List<Pie>) {
      /*  pieChart.setUsePercentValues(true)
        pieChart.description.isEnabled = false
        pieChart.setExtraOffsets(5F, 10F, 5F, 5F)

        pieChart.dragDecelerationFrictionCoef = 0.95f

        pieChart.isDrawHoleEnabled = true
        pieChart.setHoleColor(Color.WHITE)
        pieChart.setCenterTextColor(Color.WHITE)
        pieChart.setTransparentCircleColor(Color.WHITE)
        pieChart.setTransparentCircleAlpha(110)
        pieChart.holeRadius = 58f
        pieChart.transparentCircleRadius = 61f

        pieChart.setDrawCenterText(false)
        pieChart.setDrawEntryLabels(false)

        pieChart.rotationAngle = 0f
        // enable rotation of the chart by touch
        pieChart.isRotationEnabled = true
        pieChart.isHighlightPerTapEnabled = true

        pieChart.animateY(1400, Easing.EaseInOutQuad)


        val l = pieChart.legend
        l.isEnabled = true
        l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
        l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        l.orientation = Legend.LegendOrientation.VERTICAL
        l.setDrawInside(false)
        l.xEntrySpace = 7f
        l.yEntrySpace = 0f
        l.yOffset = 0f
        l.textColor = Color.WHITE

        // entry label styling
        pieChart.setEntryLabelColor(Color.WHITE)
        pieChart.setEntryLabelTextSize(12f)
        val pieList = arrayListOf<PieEntry>()
        for (pie in pies) {
            val pie = PieEntry(pie.proportion.toFloat(), pie.userName)
            pieList.add(pie)
        }

        val dataSet = PieDataSet(pieList, "")
        dataSet.valueTextColor = Color.WHITE
        val colors = arrayListOf<Int>()
        val materialColors = arrayOf(Color.rgb(200, 172, 255))

        for (element in materialColors) {
            colors.add(element)
        }

        for (element in ColorTemplate.VORDIPLOM_COLORS) {
            colors.add(element)
        }

        for (element in ColorTemplate.PASTEL_COLORS) {
            colors.add(element)
        }
        for (element in ColorTemplate.COLORFUL_COLORS) {
            colors.add(element)
        }

        dataSet.colors = colors

        val pieData = PieData(dataSet)
        pieData.setDrawValues(true)
        pieData.setValueFormatter(PercentFormatter(pieChart))
        pieChart.data = pieData
        pieChart.postInvalidate()*/
    }

    override fun setLineChart(lines: List<Line>) {
       /* lineChart.setNoDataText("没有数据哦")
        lineChart.setNoDataTextColor(Color.WHITE)
        lineChart.setDrawGridBackground(false)
        lineChart.setDrawBorders(true)
        lineChart.description.isEnabled = false
        lineChart.isDragEnabled = false
        lineChart.setTouchEnabled(true)
        lineChart.animateX(2000)
        lineChart.animateY(2000)

        val xAxis = lineChart.xAxis
        xAxis.position = XAxis.XAxisPosition.BOTTOM
        xAxis.axisMinimum = 0f
        xAxis.granularity = 1f
        xAxis.textColor = Color.WHITE
        xAxis.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return lines[value.toInt() % lines.size].dayStr
            }
        }

        val leftYAxis = lineChart.axisLeft
        val rightYaxis = lineChart.axisRight
        leftYAxis.axisMinimum = 0f
        leftYAxis.textColor = Color.WHITE
        rightYaxis.isEnabled = false

        val legend = lineChart.legend
        legend.isEnabled = false

        val values1 = arrayListOf<Entry>()

        for ((index, value) in lines.withIndex()) {
            val entry = Entry(index.toFloat(), value.increaseNum.toFloat())
            values1.add(entry)
        }

        val set1: LineDataSet

        if (lineChart.data != null &&
                lineChart.run { data.dataSetCount } > 0) {
            set1 = lineChart.data.getDataSetByIndex(0) as LineDataSet
        } else {
            set1 = LineDataSet(values1, "")
            set1.color = Color.WHITE
            set1.setCircleColor(Color.WHITE)
            set1.valueTextColor = Color.WHITE
            set1.lineWidth = 1f;//设置线宽
            set1.circleRadius = 3f;//设置焦点圆心的大小
            set1.enableDashedHighlightLine(10f, 5f, 0f);//点击后的高亮线的显示样式
            set1.highlightLineWidth = 2f;//设置点击交点后显示高亮线宽
            set1.isHighlightEnabled = true;//是否禁用点击高亮线
            set1.highLightColor = Color.RED;//设置点击交点后显示交高亮线的颜色
            set1.valueTextSize = 9f;//设置显示值的文字大小
            set1.setDrawFilled(false);//设置禁用范围背景填充
        }
        var dataSets = arrayListOf(set1)
        var lineData = LineData(dataSets as List<ILineDataSet>?)
        lineChart.data = lineData
        lineChart.postInvalidate()*/
    }

    override fun onClick(view: View?) {

    }
}
